clear all;
clc;

% ------------------------- Import Data -----------------------------------

file1 = 'sub_mp_dc_phi_120_120_310.txt';
file2 = 'sub_mp_dc_phi_126_126_400.txt';

phi1 = load(file1);
phi2 = load(file2);

act1 = load(['neg_',strrep(file1,'phi','act')]);
act2 = load(['neg_',strrep(file2,'phi','act')]);

% ------------------------- Potentials ------------------------------------
% ck = (phi1==phi1);
% [cbad,rbad] = find(ck==0);

subsamp = (145*8):(165*8);
phi1 = phi1(:,subsamp);
phi2 = phi2(:,subsamp);

[m,n] = size(phi1);

phi1(40,:) = phi1(41,:);
phi2(40,:) = phi2(41,:);

delphi = abs( 100*(phi1-phi2)./phi1 );

% ------------------------- Second Difference -----------------------------

sd1 = diff(phi1(:,1:8:end),2,2);
sd2 = diff(phi2(:,1:8:end),2,2);

% for k = 1:m
%     plot(phi1(k,:),'k-');hold on;
%     plot(phi2(k,:),'r-');hold off;
%     title(num2str(k));
%     gtmp = input('');
% end

delsd  = abs( 100*(sd1-sd2)./sd1 );

badindx = find(abs(sd1) < 1e-3);
delsd(badindx) = 0;

% ------------------------- Recruitment -----------------------------------

delact = 100*abs( (act1(:,1)-act2(:,1))./act1(:,1) );

display('potentials');
mean(mean(delphi))
std(mean(delphi,2))
display('second differences');
mean(mean(delsd))
std(mean(delsd))
display('thresholds');
mean(delact([1:39,41:100]))
std(delact([1:39,41:100]))